/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.passport.common  
 */
package com.apache.passport.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;

/**根据IP查询地址,目前使用淘宝接口
 * http://ip.taobao.com/service/getIpInfo.php
 * description:  
 * @author Hou Dayu 创建时间：2015-5-15  
 * @Copyright(c)2014:
 */
public class IPInfo {

	private static IPInfo instance;

	private IPInfo() {
	}

	public static IPInfo getInstance() {
		if (null == instance) {
			instance = new IPInfo();
		}
		return instance;
	}

	public String getIpInfo(String url, Map map) {
		String sb = new String();
		HttpClient httpClient = new HttpClient();
		GetMethod getMethod = new GetMethod(url);
		try {
			if (map.size() > 0) {
				Set keys = map.keySet();
				NameValuePair[] nameValuePair = new NameValuePair[keys.size()];
				int i = 0;
				Iterator it = keys.iterator();
				while (it.hasNext()) {
					String key = (String) it.next();
					String value = (String) map.get(key);
					nameValuePair[i] = new NameValuePair(key, value);
					i++;
				}
				getMethod.setQueryString(nameValuePair);
				int status = httpClient.executeMethod(getMethod);
				if (HttpStatus.SC_OK == status) {
					sb = inputStreamToString(getMethod.getResponseBodyAsStream());
				} else {
					sb = "{\"code\":1,\"data\":\"请求异常!\"}";
				}
			} else {
				sb = "{\"code\":1,\"data\":\"参数为空!\"}";
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * description:  将数据流转换成成string
	 * @param is
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-31
	 */
	public String inputStreamToString(InputStream is) {
		BufferedReader reader = null;
		StringBuffer responseText = null;
		String readerText = null;
		try {
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			responseText = new StringBuffer();
			readerText = reader.readLine();
			while (readerText != null) {
				responseText.append(readerText);
				responseText.append(System.getProperty("line.separator"));
				readerText = reader.readLine();
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return responseText.toString();
	}

	public static void main(String[] args) {
		String url = "http://ip.taobao.com/service/getIpInfo.php";
		Map map = new HashMap();
		map.put("ip", "118.123.19.23");
		System.out.println(IPInfo.getInstance().getIpInfo(url, map));
	}
}
